package com.wcl.nullmusic.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wcl.nullmusic.R;
import com.wcl.nullmusic.mode.Recommend_CategoryContents;
import com.wcl.nullmusic.mode.Recommend_CategoryContents_List;
import com.wcl.nullmusic.mode.Recommend_CategoryContents_List_List;
import com.wcl.nullmusic.mode.Recommend_FocusImages;
import com.wcl.nullmusic.ui.activity.AnthologyActivity;
import com.wcl.nullmusic.ui.activity.RankingActivity;
import com.wcl.nullmusic.utils.NumberUtils;
import com.wcl.nullmusic.utils.ToActivityUtils;

import java.util.List;


/**
 * Created by dllo on 16/1/15.
 */
public class RecommendRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<Recommend_CategoryContents_List> datas;
    private Context context;
    private static final int TYPE0 = 0;
    private static final int TYPE1 = 1;

    public RecommendRecyclerViewAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<Recommend_CategoryContents_List> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE0:
                View upView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recommend_recycler_up_item, parent, false);
                return new UpViewHolder(upView);
            case TYPE1:
                View downView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recommend_recycler_item, parent, false);
                return new DownViewHolder(downView);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (datas != null && datas.size() > 0) {
            final Recommend_CategoryContents_List contents_list = datas.get(position);
            switch (position) {
                case 0:
                    final Recommend_CategoryContents_List_List contents_list_list_up = contents_list.getList().get(0);
                    UpViewHolder upHolder = (UpViewHolder) holder;
                    upHolder.recommend_recycler_up_item_title.setText(contents_list_list_up.getTitle());
                    upHolder.recommend_recycler_up_item_subTitle.setText(contents_list_list_up.getSubtitle());

                    Picasso.with(context).load(contents_list_list_up.getCoverPath()).placeholder(R.mipmap.ic_classify_img07)
                            .error(R.mipmap.ic_classify_img07).into(upHolder.recommend_recycler_up_item_pic);

                    upHolder.recommend_recycler_up_item_layout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Bundle bundle=new Bundle();
                            bundle.putString("key", contents_list_list_up.getKey());
                            bundle.putString("title",contents_list_list_up.getTitle());
                            ToActivityUtils.startToActivity(context, RankingActivity.class,bundle);

                        }
                    });
                    break;
                default:
                    DownViewHolder downViewHolder = (DownViewHolder) holder;
                    downViewHolder.recommend_recycler_item_titleType.setText(contents_list.getTitle());
                    Recommend_CategoryContents_List_List contents_list_list = contents_list.getList().get(0);
                    downViewHolder.recommend_recycler_item_firstTitle.setText(contents_list_list.getTitle());
                    downViewHolder.recommend_recycler_item_firstSubTitle.setText(contents_list_list.getIntro());
                    downViewHolder.recommend_recycler_item_firstTracksCounts.setText(NumberUtils.toUnit(contents_list_list.getPlaysCounts()));
                    downViewHolder.recommend_recycler_item_firstCounts.setText(contents_list_list.getTracksCounts() + "");
                    Picasso.with(context).load(contents_list_list.getCoverMiddle()).placeholder(R.mipmap.ic_classify_img05)
                            .error(R.mipmap.ic_classify_img05).into(downViewHolder.recommend_recycler_item_firstPic);

                    contents_list_list = contents_list.getList().get(1);
                    downViewHolder.recommend_recycler_item_secondTitle.setText(contents_list_list.getTitle());
                    downViewHolder.recommend_recycler_item_secondSubTitle.setText(contents_list_list.getIntro());
                    downViewHolder.recommend_recycler_item_secondTracksCounts.setText(NumberUtils.toUnit(contents_list_list.getPlaysCounts()));
                    downViewHolder.recommend_recycler_item_secondCounts.setText(contents_list_list.getTracksCounts() + "");
                    Picasso.with(context).load(contents_list_list.getCoverMiddle()).placeholder(R.mipmap.ic_classify_img06)
                            .error(R.mipmap.ic_classify_img06).into(downViewHolder.recommend_recycler_item_secondPic);

                    contents_list_list = contents_list.getList().get(2);
                    downViewHolder.recommend_recycler_item_thirdTitle.setText(contents_list_list.getTitle());
                    downViewHolder.recommend_recycler_item_thirdSubTitle.setText(contents_list_list.getIntro());
                    downViewHolder.recommend_recycler_item_thirdTracksCounts.setText(NumberUtils.toUnit(contents_list_list.getPlaysCounts()));
                    downViewHolder.recommend_recycler_item_thirdCounts.setText(contents_list_list.getTracksCounts() + "");
                    Picasso.with(context).load(contents_list_list.getCoverMiddle()).placeholder(R.mipmap.ic_classify_img07)
                            .error(R.mipmap.ic_classify_img07).into(downViewHolder.recommend_recycler_item_thirdPic);

                    downViewHolder.recommend_recycler_item_firstLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Bundle bundle = new Bundle();
                            bundle.putString("album", contents_list.getList().get(0).getAlbumId() + "");
                            bundle.putString("position", 1 + "");
                            bundle.putString("title", contents_list.getList().get(0).getTitle());
                            ToActivityUtils.startToActivity(context, AnthologyActivity.class, bundle);
                        }
                    });
                    downViewHolder.recommend_recycler_item_secondLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Bundle bundle = new Bundle();
                            bundle.putString("album", contents_list.getList().get(1).getAlbumId() + "");
                            bundle.putString("position", 2 + "");
                            bundle.putString("title", contents_list.getList().get(1).getTitle());
                            ToActivityUtils.startToActivity(context, AnthologyActivity.class, bundle);
                        }
                    });
                    downViewHolder.recommend_recycler_item_thirdLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Bundle bundle = new Bundle();
                            bundle.putString("album", contents_list.getList().get(2).getAlbumId() + "");
                            bundle.putString("position", 3 + "");
                            bundle.putString("title", contents_list.getList().get(2).getTitle());
                            ToActivityUtils.startToActivity(context, AnthologyActivity.class, bundle);

                        }
                    });
                    break;
            }

        }

    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return TYPE0;
            default:
                return TYPE1;
        }
    }

//    @Override
//    public void onBindViewHolder(ViewHolder holder, final int position) {
//        if (datas != null && datas.size() > 0) {
//
//            Recommend_CategoryContents_List contents_list = datas.get(position + 1);
//            holder.recommend_recycler_item_titleType.setText(contents_list.getTitle());
//            Recommend_CategoryContents_List_List contents_list_list = contents_list.getList().get(0);
//            holder.recommend_recycler_item_firstTitle.setText(contents_list_list.getTitle());
//            holder.recommend_recycler_item_firstSubTitle.setText(contents_list_list.getIntro());
//            holder.recommend_recycler_item_firstTracksCounts.setText(contents_list_list.getPlaysCounts() + "");
//            holder.recommend_recycler_item_firstCounts.setText(contents_list_list.getTracksCounts() + "");
//            Picasso.with(context).load(contents_list_list.getCoverSmall()).placeholder(R.mipmap.ic_classify_img05)
//                    .error(R.mipmap.ic_classify_img05).into(holder.recommend_recycler_item_firstPic);
//
//            contents_list_list = contents_list.getList().get(1);
//            holder.recommend_recycler_item_secondTitle.setText(contents_list_list.getTitle());
//            holder.recommend_recycler_item_secondSubTitle.setText(contents_list_list.getIntro());
//            holder.recommend_recycler_item_secondTracksCounts.setText(contents_list_list.getPlaysCounts() + "");
//            holder.recommend_recycler_item_secondCounts.setText(contents_list_list.getTracksCounts() + "");
//            Picasso.with(context).load(contents_list_list.getCoverSmall()).placeholder(R.mipmap.ic_classify_img06)
//                    .error(R.mipmap.ic_classify_img06).into(holder.recommend_recycler_item_secondPic);
//
//            contents_list_list = contents_list.getList().get(2);
//            holder.recommend_recycler_item_thirdTitle.setText(contents_list_list.getTitle());
//            holder.recommend_recycler_item_thirdSubTitle.setText(contents_list_list.getIntro());
//            holder.recommend_recycler_item_thirdTracksCounts.setText(contents_list_list.getPlaysCounts() + "");
//            holder.recommend_recycler_item_thirdCounts.setText(contents_list_list.getTracksCounts() + "");
//            Picasso.with(context).load(contents_list_list.getCoverSmall()).placeholder(R.mipmap.ic_classify_img07)
//                    .error(R.mipmap.ic_classify_img07).into(holder.recommend_recycler_item_thirdPic);
//
//            holder.recommend_recycler_item_firstLayout.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Bundle bundle = new Bundle();
//                    bundle.putString("album", datas.get(position + 1).getList().get(0).getAlbumId() + "");
//                    bundle.putString("position", 1 + "");
//                    bundle.putString("title", datas.get(position + 1).getList().get(0).getTitle());
//                    ToActivityUtils.startToActivity(context, AnthologyActivity.class, bundle);
//                }
//            });
//            holder.recommend_recycler_item_secondLayout.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Bundle bundle = new Bundle();
//                    bundle.putString("album", datas.get(position + 1).getList().get(1).getAlbumId() + "");
//                    bundle.putString("position", 2 + "");
//                    bundle.putString("title", datas.get(position + 1).getList().get(1).getTitle());
//                    ToActivityUtils.startToActivity(context, AnthologyActivity.class, bundle);
//                }
//            });
//            holder.recommend_recycler_item_thirdLayout.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Bundle bundle = new Bundle();
//                    bundle.putString("album", datas.get(position + 1).getList().get(2).getAlbumId() + "");
//                    bundle.putString("position", 3 + "");
//                    bundle.putString("title", datas.get(position + 1).getList().get(2).getTitle());
//                    ToActivityUtils.startToActivity(context, AnthologyActivity.class, bundle);
//                }
//            });
//
//        }
//
//    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }


    class UpViewHolder extends RecyclerView.ViewHolder {
        private ImageView recommend_recycler_up_item_pic;
        private TextView recommend_recycler_up_item_title, recommend_recycler_up_item_subTitle;
        private RelativeLayout recommend_recycler_up_item_layout;

        public UpViewHolder(View itemView) {
            super(itemView);
            recommend_recycler_up_item_pic = (ImageView) itemView.findViewById(R.id.recommend_recycler_up_item_pic);

            recommend_recycler_up_item_title = (TextView) itemView.findViewById(R.id.recommend_recycler_up_item_title);
            recommend_recycler_up_item_subTitle = (TextView) itemView.findViewById(R.id.recommend_recycler_up_item_subTitle);

            recommend_recycler_up_item_layout= (RelativeLayout) itemView.findViewById(R.id.recommend_recycler_up_item_layout);
        }
    }

    class DownViewHolder extends RecyclerView.ViewHolder {
        private TextView recommend_recycler_item_titleType, recommend_recycler_item_firstTitle,
                recommend_recycler_item_firstSubTitle, recommend_recycler_item_firstTracksCounts,
                recommend_recycler_item_firstCounts, recommend_recycler_item_secondTitle,
                recommend_recycler_item_secondSubTitle, recommend_recycler_item_secondTracksCounts,
                recommend_recycler_item_secondCounts, recommend_recycler_item_thirdTitle,
                recommend_recycler_item_thirdSubTitle, recommend_recycler_item_thirdTracksCounts,
                recommend_recycler_item_thirdCounts;
        private ImageView recommend_recycler_item_firstPic, recommend_recycler_item_secondPic,
                recommend_recycler_item_thirdPic;
        private RelativeLayout recommend_recycler_item_firstLayout, recommend_recycler_item_secondLayout,
                recommend_recycler_item_thirdLayout;

        public DownViewHolder(View itemView) {
            super(itemView);
            recommend_recycler_item_titleType = (TextView) itemView.findViewById(R.id.recommend_recycler_item_titleType);
            recommend_recycler_item_firstTitle = (TextView) itemView.findViewById(R.id.recommend_recycler_item_firstTitle);
            recommend_recycler_item_firstSubTitle = (TextView) itemView.findViewById(R.id.recommend_recycler_item_firstSubTitle);
            recommend_recycler_item_firstTracksCounts = (TextView) itemView.findViewById(R.id.recommend_recycler_item_firstTracksCounts);
            recommend_recycler_item_firstCounts = (TextView) itemView.findViewById(R.id.recommend_recycler_item_firstCounts);
            recommend_recycler_item_secondTitle = (TextView) itemView.findViewById(R.id.recommend_recycler_item_secondTitle);
            recommend_recycler_item_secondSubTitle = (TextView) itemView.findViewById(R.id.recommend_recycler_item_secondSubTitle);
            recommend_recycler_item_secondTracksCounts = (TextView) itemView.findViewById(R.id.recommend_recycler_item_secondTracksCounts);
            recommend_recycler_item_secondCounts = (TextView) itemView.findViewById(R.id.recommend_recycler_item_secondCounts);
            recommend_recycler_item_thirdTitle = (TextView) itemView.findViewById(R.id.recommend_recycler_item_thirdTitle);
            recommend_recycler_item_thirdSubTitle = (TextView) itemView.findViewById(R.id.recommend_recycler_item_thirdSubTitle);
            recommend_recycler_item_thirdTracksCounts = (TextView) itemView.findViewById(R.id.recommend_recycler_item_thirdTracksCounts);
            recommend_recycler_item_thirdCounts = (TextView) itemView.findViewById(R.id.recommend_recycler_item_thirdCounts);

            recommend_recycler_item_firstPic = (ImageView) itemView.findViewById(R.id.recommend_recycler_item_firstPic);
            recommend_recycler_item_secondPic = (ImageView) itemView.findViewById(R.id.recommend_recycler_item_secondPic);
            recommend_recycler_item_thirdPic = (ImageView) itemView.findViewById(R.id.recommend_recycler_item_thirdPic);

            recommend_recycler_item_firstLayout = (RelativeLayout) itemView.findViewById(R.id.recommend_recycler_item_firstLayout);
            recommend_recycler_item_secondLayout = (RelativeLayout) itemView.findViewById(R.id.recommend_recycler_item_secondLayout);
            recommend_recycler_item_thirdLayout = (RelativeLayout) itemView.findViewById(R.id.recommend_recycler_item_thirdLayout);
        }
    }
}
